package cn.com.haopy.yue.ai.core.system;


import cn.com.haopy.yue.ai.core.component.CacheComponent;
import cn.com.haopy.yue.ai.core.utils.RedisCachePreFix;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 系统设置
 */
@Component
public class SystemConfig {
    @Autowired
    private CacheComponent tmp;
    private static CacheComponent cache;

    //    static {
//        cache = BeanUtil.getBean(CacheComponent.class);
//    }
    @PostConstruct
    public void init() {
        cache = tmp;
    }

    public final static String SYSTEM_USER_PASSWORD_COMPLEXITY_CHECK = "system_user_password_complexity_check";

    public final static String SYSTEM_USER_PASSWORD_FORCED_EXPIRED_ENABLE = "system_password_forced_expired_enable";

    public final static String SYSTEM_USER_PASSWORD_FORCED_EXPIRED_DAYS = "system_password_forced_expired_days";

    public final static String SYSTEM_USER_LOGIN_MAX_FAILED_COUNT = "system_user_login_max_failed_count";

    public final static String SYSTEM_USER_LOGIN_LOCK_TIMEOUT = "system_user_login_lock_timeout";

    public final static String SYSTEM_INTFC_SECRET_AES_KEY = "system_intfc_secret_ase_key";

    public final static String SYSTEM_UPLOAD_FILE_FORMAT = "system_upload_file_format";


    public final static String SYSTEM_RISK_HUMAN_SOURCE_COUNT = "system_risk_human_source_count";

    public final static String SYSTEM_RISK_LEVEL_LIST = "system_risk_level_list";

    //总结--默认值
    public final static String SYSTEM_SUMMARY_RESULT_CONTENT = "system_summary_result_content";


    //用户协议
    public final static String USER_RISK_WX_PROTOCOL = "user_risk_wx_protocol";

    //隐私政策
    public final static String USER_RISK_WX_PRIVACY = "user_risk_wx_privacy";

    //会员费时长
    public final static String VIP_CHARGE_MONTH = "vip_charge_month";

    //金额--会员-充值
    public final static String VIP_CHARGE_MONEY = "vip_charge_money";


    public static Integer getSystemUserPasswordForcedExpiredDays() {
        return getConfigInt(SYSTEM_USER_PASSWORD_FORCED_EXPIRED_DAYS);
    }


    //所有的配置均保存在该 HashMap 中
//    private static Map<String, String> SYSTEM_CONFIGS = new HashMap<>();

    private static Map<String, String> getConfigMap() {
        return cache.getHashAll(RedisCachePreFix.SYSCONFIG.getCachePreFix());
    }

    private static String getConfig(String keyName) {
        Map<String, String> configs = getConfigMap();
        return configs.get(keyName);
    }

    private static Integer getConfigInt(String keyName) {
        return Integer.parseInt(getConfig(keyName));
    }

    private static Long getConfigLong(String keyName) {
        return Long.parseLong(getConfig(keyName));
    }

    private static Boolean getConfigBoolean(String keyName) {
        return Boolean.valueOf(getConfig(keyName));
    }

    private static BigDecimal getConfigBigDec(String keyName) {
        return new BigDecimal(getConfig(keyName));
    }

    public static void setConfigs(Map<String, String> configs) {
        cache.putHashAll(RedisCachePreFix.SYSCONFIG.getCachePreFix(), configs, 1000 * 24 * 60 * 60);
    }

    public static void updateConfigs(Map<String, String> data) {
        Map<String, String> configs = getConfigMap();
        configs.putAll(data);
        setConfigs(configs);
    }

    public static Boolean getSystemUserPasswordComplexityCheck() {
        return getConfigBoolean(SYSTEM_USER_PASSWORD_COMPLEXITY_CHECK);
    }

    public static Boolean getSystemUserPasswordForcedExpiredEnable() {
        return getConfigBoolean(SYSTEM_USER_PASSWORD_FORCED_EXPIRED_ENABLE);
    }


    public static Integer getSystemUserLoginMaxFailedCount() {
        return getConfigInt(SYSTEM_USER_LOGIN_MAX_FAILED_COUNT);
    }

    public static Integer getSystemUserLoginLockTimeout() {
        return getConfigInt(SYSTEM_USER_LOGIN_LOCK_TIMEOUT);
    }


    public static String getSystemIntfcSecretAesKey() {
        return getConfig(SYSTEM_INTFC_SECRET_AES_KEY);
    }

    public static String getSystemUploadFileFormat() {
        return getConfig(SYSTEM_UPLOAD_FILE_FORMAT);
    }


    public static Integer getSystemRiskHumanSourceCount() {
        return getConfigInt(SYSTEM_RISK_HUMAN_SOURCE_COUNT);
    }

    public static String getSystemRiskLevelList() {
        return getConfig(SYSTEM_RISK_LEVEL_LIST);
    }

    public static String getSummaryResultContent() {
        return getConfig(SYSTEM_SUMMARY_RESULT_CONTENT);
    }


    public static String getUserRiskWxProtocol() {
        return getConfig(USER_RISK_WX_PROTOCOL);
    }

    public static String getUserRiskWxPrivacy() {
        return getConfig(USER_RISK_WX_PRIVACY);
    }


    public static Integer getVipchargeMonth() {
        return getConfigInt(VIP_CHARGE_MONTH);
    }


    public static BigDecimal getVipChargeMoney() {
        return getConfigBigDec(VIP_CHARGE_MONEY);
    }


}